<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once(ROOT_PATH."class/messages.class.php");
require_once(ROOT_PATH."class/team.class.php");

$team = new team;
$dateformat = 'yyyy-mm-dd';

if ($CURRENT_USER->groupid == GROUP_ADMIN) {
	$teamlist = $loader->loadList($team);	
} else {
	$team = new team;
	$team->id = $CURRENT_USER->teamid;
	$loader->get($team);
	$teamlist = array($team);
	$teamobj=$team;
}

if (($param['op']=='act' or $param['op']=='deact') and $flow_ok) {
	
	foreach($param['itemid'] as $id) {
		$msg = new messages;
		$msg->id=$id;
		$ret = $loader->get($msg);
		if ($ret) {
			if ($param['op']=='act') {
				$msg->status=STATUS_ACTIVE;
			} else {
				$msg->status=STATUS_INACTIVE;
			}
			$loader->update($msg);
		}
	}
	$message=$LANG['updated'];
} elseif ($param['op']=='add' and $flow_ok) {
	$msg = new messages;
	$msg->teamid=$param['teamid'];
	
	$df=split('[/.-]',$dateformat);
	$showstart = split('[/.-]',$param['showstart']);
	$showend = split('[/.-]',$param['showend']);

	for ($i=0;$i<count($df);$i++) {
		if (strpos($df[$i],"yyyy")!==false) {
			$sa=$showstart[$i];
		} elseif (strpos($df[$i],"mm")!==false) {
			$sm=$showstart[$i];
		} elseif (strpos($df[$i],"dd")!==false) {
			$sd=$showstart[$i];
		}
	}
	
	for ($i=0;$i<count($df);$i++) {
		if (strpos($df[$i],"yyyy")!==false) {
			$ea=$showend[$i];
		} elseif (strpos($df[$i],"mm")!==false) {
			$em=$showend[$i];
		} elseif (strpos($df[$i],"dd")!==false) {
			$ed=$showend[$i];
		}
	}

	$showstart = util::formatDate(mktime(0,0,0,$sm,$sd,$sa));
	$showend = util::formatDate(mktime(23,59,59,$em,$ed,$ea));
	$msg->showstart=$showstart;
	$msg->showend=$showend;
	$msg->message=$param['message'];
	$loader->save($msg);
} elseif ($param['op']=='edit' and $flow_ok) {
	$msg = new messages;
	$msg->id=$param['id'];
	$ret = $loader->get($msg);
	if ($ret) {
		$msg->teamid=$param['teamid'];

		$df=split('[/.-]',$dateformat);
		$showstart = split('[/.-]',$param['showstart']);
		$showend = split('[/.-]',$param['showend']);
	
		for ($i=0;$i<count($df);$i++) {
			if (strpos($df[$i],"yyyy")!==false) {
				$sa=$showstart[$i];
			} elseif (strpos($df[$i],"mm")!==false) {
				$sm=$showstart[$i];
			} elseif (strpos($df[$i],"dd")!==false) {
				$sd=$showstart[$i];
			}
		}
		
		for ($i=0;$i<count($df);$i++) {
			if (strpos($df[$i],"yyyy")!==false) {
				$ea=$showend[$i];
			} elseif (strpos($df[$i],"mm")!==false) {
				$em=$showend[$i];
			} elseif (strpos($df[$i],"dd")!==false) {
				$ed=$showend[$i];
			}
		}
		$showstart = util::formatDate(mktime(0,0,0,$sm,$sd,$sa));
		$showend = util::formatDate(mktime(23,59,59,$em,$ed,$ea));
		$msg->showstart=$showstart;
		$msg->showend=$showend;
		$msg->message=$param['message'];		
		$loader->update($msg);
		$message=$LANG['updated'];
	}
} elseif ($param['op']=='remove' and $flow_ok) {
	
	foreach($param['itemid'] as $id) {
		$msg = new messages;
		$msg->id=$id;
		$ret = $loader->delete($msg);
	}
}

$msg = new messages;
if ($CURRENT_USER->groupid != GROUP_ADMIN) {
	$msg->teamid=$CURRENT_USER->teamid;
	$list = $loader->loadListBy($msg,$msg->listByTeam());
} else {
	$list = $loader->loadList($msg);	
}

foreach ($list as $key => $obj) {
	foreach ($teamlist as $tkey => $tobj) {
		if ($obj->teamid == $tobj->id) {
			$obj->team=$tobj;	
		}	
	}
}


$tm->assign('dateformat',$dateformat);
$tm->assign('list',$list);
$tm->assign('teamlist',$teamlist);
$content = $tm->fetch('messagelist.tpl');

?>
